package cn.whit.blog.service.impl;

import java.sql.SQLException;
import java.util.List;

import cn.whit.blog.dao.TcommitDao;
import cn.whit.blog.domain.Tblog;
import cn.whit.blog.domain.Tcommit;
import cn.whit.blog.service.TcommitService;
import cn.whit.blog.util.BeanFactory;
import cn.whit.blog.util.DynamicQuery;
import cn.whit.blog.util.PageModel;

public class TcommitServiceImpl implements TcommitService{
	
	TcommitDao tcommitDao = (TcommitDao)BeanFactory.createObject("tcommitDao");
	
	@Override
	public PageModel findByPageAndQuery(int startIndex, int pageSize, DynamicQuery query) throws Exception {
		//startIndex 从第一页，索引 1 开始
		int totalRecords = tcommitDao.findTotalRecordsAndQuery(query);
		PageModel pm = new PageModel(startIndex,totalRecords,pageSize);
		List<Tcommit> list = tcommitDao.findAllWithPageAndQuery(pm.getStartIndex(),pm.getPageSize(),query);
		pm.setList(list);
		//3_关联url
		pm.setUrl("TcommitServlet?method=tcommitList");
		return pm;
	}
	
	@Override
	public List<Tcommit> findAllWithPageAndQuery(int startIndex, int pageSize, DynamicQuery query) throws Exception {
		return tcommitDao.findAllWithPageAndQuery(startIndex, pageSize, query);
	}

	@Override
	public int findTotalRecordsAndQuery(DynamicQuery query) throws Exception {
		return tcommitDao.findTotalRecordsAndQuery(query);
	}

	@Override
	public void save(Tcommit tcommit) throws SQLException {
		tcommitDao.save( tcommit);
	}

	@Override
	public Tcommit findById(String id) throws SQLException {
		return tcommitDao.findById(id);
	}
	
	public static void main(String[] args) throws Exception {
		
		TcommitServiceImpl tc = new TcommitServiceImpl();
		
		int startIndex = 0;
		int pageSize = 5;
		DynamicQuery query = new DynamicQuery();;
//		List<Tcommit> ones = tc.findAllWithPageAndQuery(startIndex, pageSize, query);
//		int count = tc.findTotalRecordsAndQuery(query);
//		System.out.println("count=" + count);
//		System.out.println(ones.size());
		
		 
		
	}

	@Override
	public PageModel findAllByBlogIdWithPageAndQuery(int startIndex, int pageSize, DynamicQuery query, String blogid) throws Exception {
		//startIndex 从第一页，索引 1 开始
		int totalRecords = tcommitDao.findTotalRecordsByBlogIdAndQuery(query,blogid);
		PageModel pm = new PageModel(startIndex,totalRecords,pageSize);
		List<Tcommit> list = tcommitDao.findAllByBlogIdWithPageAndQuery(pm.getStartIndex(),pm.getPageSize(),query,blogid);
		pm.setList(list);
		//3_关联url
		pm.setUrl("TcommitServlet?method=tcommitList");
		return pm;
		
	}
	
	 

}
